/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.util.Random;

/**
 * <p>Project: app2022 - Ex3
 * <p>Powered by webrx On 2022-01-24 11:47:48
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex3 {
    public static void main(String[] args) throws Exception {
        var p = "pok.jpg";
        var i = ImageIO.read(new File(p));
        var g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        //字体
        var font = new Font("微软雅黑", Font.BOLD, 50);
        var is = Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/f1.ttf");
        font = Font.createFont(Font.TRUETYPE_FONT, is);
        font = font.deriveFont(Font.BOLD, 50f);
        g.setFont(font);

        //颜色
        Random rand = new Random();

        var c = rand.nextBoolean() ? new Color(255, 255, 255, 120) : new Color(0, 0, 0, 120);
        g.setColor(c);

        //水印文字内容，控制位置
        var fm = g.getFontMetrics(font);
        String text = "版权所有 13014577033";
        int tw = fm.stringWidth(text);
        int x = 10;
        int y = 10;
        int s = 10;
        //左上角
        x = 0 + s;
        y = fm.getAscent() - fm.getDescent() + s;

        g.drawString(text, x, y);
        g.dispose();
        ImageIO.write(i, "jpg", new File("pok2.jpg"));
    }
}
